package com.jk.controller;


import com.jk.entity.*;
import com.jk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("app/pay")
public class TestController {
    @Autowired
    private UserService userService;

    //测试
    @GetMapping("test")
    public Map<String, String> testData() {

        String hellow = userService.sayHello("张三");
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("result", hellow);
        return resultMap;
    }

    //判断密码还有几次机会一共三次
    @GetMapping("panduanmima")
    public Integer panduanmima(HttpServletRequest request) {
        User user = new User();
        user.setUserName(request.getParameter("userName"));
        User denglu = userService.loginout(user);
        if (denglu.getLoginAstrict() == null) {
            return 8;
        }
        if (denglu.getLoginAstrict() == 0) {
            return 2;
        }
        if (denglu.getLoginAstrict() == 1) {
            return 1;
        }
        if (denglu.getLoginAstrict() == 2) {
            return 0;
        }
        return 4;
    }

    //登陆
    private  HttpSession session2 = null;
    @GetMapping("login")
    public Integer loginout(HttpServletRequest request) {
        User user = new User();
        user.setUserName(request.getParameter("userName"));
        user.setUserPassword(request.getParameter("userPassword"));
        User denglu = userService.loginout(user);
        if (denglu == null) {
            return 1;
        }
        if (!denglu.getUserPassword().equals(user.getUserPassword())) {
            Integer aa = userService.xiugaidnelucishu(denglu.getUserId());
            return 2;
        } else {
            //session2 = request.getSession();session2.setAttribute("login",denglu);
            request.getSession().getServletContext().setAttribute("login",denglu);
            return 3;
        }


    }

    //验证身份证
    @PostMapping("shengfenzhengyancheng")
    public int shengfenzhengyancheng(HttpServletRequest request) {
        User user = new User();

        user.setIdCard(request.getParameter("idCard"));
        User denglu = userService.shengfenzhengyancheng(user);
        if (denglu == null) {
            return 1;
        }
        return 2;
    }

    //用户注册
    @PostMapping("inset")
    public Integer addzhuceh(HttpServletRequest request){
        User user = new User();
        String uuid=UUID.randomUUID().toString().replace("-","");
        user.setUserId(uuid);//id
        user.setUserName(request.getParameter("phoneNumber"));//账号
        user.setUserPassword(request.getParameter("userPassword"));//密码
        user.setRealName(request.getParameter("realName"));//真实姓名
        user.setPhoneNumber(request.getParameter("phoneNumber"));//手机号码
        user.setAstrict(0);//发送短信次数限制
        user.setLoginAstrict(0);//登录错误次数限制
        user.setUserType(Integer.parseInt(request.getParameter("userType")));//用户类型
        user.setIdCard(request.getParameter("idCard"));//身份证号
        user.setHome(request.getParameter("home"));//家庭住址
        user.setPayPassword(request.getParameter("payPassword"));//支付密码
        System.out.println(user.toString());
        Integer aa = userService.addzhuceh(user);
        return 1;
    }
    //短信验证
    private  HttpSession session = null;
    @RequestMapping("duanxin")
    public Integer duanxinyanzhengm(HttpServletRequest request){
        int msg= (int) ((Math.random() * 9 + 1) * 100000);
        System.out.println("短信验证码："+msg);
        request.getSession().getServletContext().setAttribute("username",msg);
        User user = new User();
        dx dxx = new dx();
        user.setPhoneNumber(request.getParameter("phoneNumber"));
        User usersj = userService.shoujihaochaxun(user);
        String shoujihao = user.getPhoneNumber();

        dx dsa = userService.chaxunhsoujihao(shoujihao);
        //判断user表里面有没有这个手机号有就不让注册
       if(usersj != null){
            return 1;
            //判断一下短信表里面有没有这个这个手机号有就判断发送几次验证码啦
        }else if(dsa!=null){
           //短信表里面如果没有的话就给他加上一条正常
            if(dsa.getCishu()==3){
                return 9;
            }else{
                //修改短信次数在发送验证码
               // Integer z = userService.updateduanxincishu(shoujihao);
              //  IndustrySMS.execute((String) user.getPhoneNumber(),(int) msg);
            }
        }else{
           //新增验证码短信数据在发送验证码
           String uuid=UUID.randomUUID().toString().replace("-","");
           dxx.setId(uuid);
           dxx.setShoujihao(shoujihao);
           dxx.setCishu(1);
          // Integer j = userService.insertdaunxian(dxx);
          // IndustrySMS.execute((String) user.getPhoneNumber(),(int) msg);

        }
       return 8897;
    }


    //判断验证码是不是正确
  @RequestMapping("panduanyanzhenma")
    public Integer puanduanyanzhengmashibushizhengque(HttpServletRequest request){
        String yzm = request.getParameter("yzm");
        System.out.print("前台传来的验证码："+yzm);
      Integer username =(Integer)request.getSession().getServletContext().getAttribute("username");
      System.out.println(username.toString());

        if(yzm != null && yzm.equals(username.toString())){
            return 1;
        }else{
            return 2;
        }
    }
    /**
     * <pre>selectUserTree(人员树)
     * 创建人：张见军
     * 创建时间：2017年11月27日 下午7:15:57
     * 修改人：张见军
     * 修改时间：2017年11月27日 下午7:15:57
     * 修改备注： </pre>
     *
     */
    @GetMapping("renyuanshu")
    public List<Map> selectDeptTree(){
        List<Map> Dept= userService.selectDeptTree();
        List<Map> queryUserTree= userService.selectUserTreeDept();
        Dept.addAll(queryUserTree);
        return Dept;
    }
    //查询应用详情
    @GetMapping("yingyongxingqing")
    public  List<Use> yingyongxingqing(HttpServletRequest request){
        //User login1 = (User)session2.getAttribute("login");
        User login =(User) request.getSession().getServletContext().getAttribute("login");
        List<Use> lists = userService.yingyongxingqing(login.getUserId());
        return lists;
    }
    //生成id
    @GetMapping("shengchengid")
    public  String  shengchengid(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHssmm");
        String format = simpleDateFormat.format(new Date());
        int msg= (int) ((Math.random() * 9 + 1) * 10);
      String appid1 = format+msg;
        return appid1;
    }
    //生成公钥
    @PostMapping("shengchengongyao")
    public  String  shengchengongyao(HttpServletRequest request){
        Use use = new Use();
        use.setAppid(request.getParameter("appid"));
        System.out.println(use.toString());
        String md532 = Md5Util.getMd532(Md5Util.getMd532(use.getAppid()));
        return md532;
    }
    @PostMapping("xingzheng")
    public  String  addsxingzheng(Use use,HttpServletRequest request){
        String uuid=UUID.randomUUID().toString().replace("-","");
        use.setUseid(uuid);
       // User login1 = (User)session2.getAttribute("login");
        User login =(User) request.getSession().getServletContext().getAttribute("login");
        use.setUserid(login.getUserId());
        use.setUsestatus(1);
        userService.addsxingzheng(use);
return "2222";
    }
    //判断秘钥
    @PostMapping("panduanmiyao")
    public Integer panduanmiyao(Use use){
        //生成订单id 把这个生成到前台现在先放着以后换
        String ddstatus=UUID.randomUUID().toString().replace("-","");
        //根据传过来个usrAppid查询出来一条数据看看和前台传过来的匹不匹配
        Use Appid = userService.panduanmiyuao(use);
        if(Appid == null){
            return 1;//appid不存在
        }else if(use.getPublickey() == null||!use.getPublickey().equals(Appid.getPublickey()) ){
            return 2;//秘钥不对
        }else{
        Order ddstatusa = userService.chaxundingdianzhuangtai(ddstatus);
        if(ddstatusa==null){
            return 3;//没有订单可以支付
        }else  if(ddstatusa.getDdstatus()==1){
            return 4;//有订单没有支付
        }else  if(ddstatusa.getDdstatus()==2){
            return 5;//支付不成功
        }else  if(ddstatusa.getDdstatus()==3){
            return 6;//正在支付
        }else{
            return 7;//已经支付
        }

        }
    }
    //删除应用
    @PostMapping("delectzjj")
    public Integer delectzjj(String useid){
      Integer shanc =  userService.delectzjj(useid);
        return shanc;
    }

    /**支付的时候把支付的商品信息保存一下
     * 在修改给人账户的余额和商家的余额
     * 在修改支付状态
     * @param order
     * @return
     */
    @PostMapping("zhifushangpain")

    public Integer zhifushangpain(Order order,HttpServletRequest request){
        //这个以后改成前台传过来的和上面的流水号id 保持一致
        String ddstatus=UUID.randomUUID().toString().replace("-","");
        //先判断前台传过来的流水号在数据库中有没有如果有就不添加 没有在添加
        Order ddstatusa = userService.chaxundingdianzhuangtai(ddstatus);
        //获取登录的对象
        User login =(User) request.getSession().getServletContext().getAttribute("login");
        if(ddstatusa.getDdid() !=null){//判断这个商品是不是已经添加上如果没有就添加
        }else{//没有就添加
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        String format = simpleDateFormat.format(date);
        //前台生成的流水号 不用上面那个 现在就先写死
        order.setDdid(ddstatus);//id
        order.setDdtime(format);
        //User login1 = (User)session2.getAttribute("login");//获取登录的用户id
            order.setGluserid(login.getUserId());//用户id
          Integer shangpinXX = userService.tianjaishangpainxianxin(order);
         }
        //去查询用户余额根据登录的
        return  1;
    }
}
